package forum.service.rest;

import com.foreveross.springboot.dubbo.utils.Payload;
import forum.domain.RoleResources;
import forum.domain.Token;
import forum.domain.UserRole;
import forum.service.rest.api.RoleResourcesRestService;
import forum.service.rpc.api.RoleResourcesService;
import forum.service.rpc.api.TokenService;
import forum.service.rpc.api.UserRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.QueryParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/1.
 */
@Service("roleResourcesRestService")
@RestController
@RequestMapping(value = "/apis/roleResources")
public class RoleResourcesRestServiceImpl implements RoleResourcesRestService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RoleResourcesService roleResourcesService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private TokenService tokenService;

    Map<String,String> error = new HashMap<String, String>();
    Map<String,Object> success = new HashMap<String, Object>();

    @RequestMapping(value = "/getListByRolename", method = RequestMethod.GET)
    public Payload getRoleResourcesListByUsername(@QueryParam("rolename") @DefaultValue("rolename=admin") String rolename,
                                                  @QueryParam("token") @DefaultValue("null") String token) {

        List<Token> tokenlist = new ArrayList<Token>();
        if (token == null) {
            error.put("msg","没有登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("没有登陆");
        } else {
            tokenlist = (List<Token>) tokenService.findByTokenname(token);
        }
        if (tokenlist != null && tokenlist.size() > 0) {


            String loginname = tokenlist.get(0).getTokenusername().toString().trim();

            List<UserRole> roleList = (List<UserRole>) userRoleService.getUserRoleListByUsername(loginname);
            if (roleList.size() > 0 && roleList != null) {
                logger.info("你的角色<------>" + roleList.get(0).getRole().getRolename().toString().trim() + "<------>");
                List<RoleResources> resourcesList = (List<RoleResources>) roleResourcesService.getRoleResourcesListByRolename(roleList.get(0).getRole().getRolename().toString().trim());
                if (resourcesList != null && resourcesList.size() > 0) {
                    for (RoleResources list : resourcesList) {
                        logger.info("------目前有的权限" + list.getResources().getResourcesname() + "////////////");
                        if (list.getResources().getResourcesname().equals("apis:resources:updateById") || list.getResources().getResourcesname().equals("resources")) {
                            logger.info("你刚好有权限标志<------>apis:resources:updateById<------>||<------>resources<------>");
                            //这里开始搞事情
                            success.put("content",roleResourcesService.getRoleResourcesListByRolename(rolename));
                            success.put("success",1);
                            return new Payload(success);
//                            return new Payload(roleResourcesService.getRoleResourcesListByRolename(rolename));
                        }
                    }
                } else {
                    error.put("msg","权限为空");
                    error.put("fail","0");
                    return new Payload(error);
//                    return new Payload("权限为空");
                }
            } else {
                error.put("msg","角色为空");
                error.put("fail","0");
                return new Payload(error);
//                return new Payload("角色为空");
            }

        } else {
            error.put("msg","token 过期或者token不存在 请重新登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("token 过期或者token不存在 请重新登陆");
        }
        error.put("msg","不存在/apis/roleResources/getListByRolename访问权限");
        error.put("fail","0");
        return new Payload(error);
//        return new Payload("不存在/apis/roleResources/getListByRolename访问权限");
    }

    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public Payload getRoleResourcesList(@QueryParam("page") @DefaultValue("0") int page,
                                        @QueryParam("size") @DefaultValue("50") int size,
                                        @QueryParam("sort") @DefaultValue("sort=user_role_id,desc") String sort,
                                        @QueryParam("operation") @DefaultValue("operation=eq") String operation,
                                        @QueryParam("key") @DefaultValue("key=user_role_id") String key,
                                        @QueryParam("value") @DefaultValue("1") String value,
                                        @QueryParam("token") @DefaultValue("null") String token) {
        List<Token> tokenlist = new ArrayList<Token>();
        if (token == null) {
            error.put("msg","没有登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("没有登陆");
        } else {
            tokenlist = (List<Token>) tokenService.findByTokenname(token);
        }
        if (tokenlist != null && tokenlist.size() > 0) {


            String loginname = tokenlist.get(0).getTokenusername().toString().trim();

            List<UserRole> roleList = (List<UserRole>) userRoleService.getUserRoleListByUsername(loginname);
            if (roleList.size() > 0 && roleList != null) {
                logger.info("你的角色<------>" + roleList.get(0).getRole().getRolename().toString().trim() + "<------>");
                List<RoleResources> resourcesList = (List<RoleResources>) roleResourcesService.getRoleResourcesListByRolename(roleList.get(0).getRole().getRolename().toString().trim());
                if (resourcesList != null && resourcesList.size() > 0) {
                    for (RoleResources list : resourcesList) {
                        logger.info("------目前有的权限" + list.getResources().getResourcesname() + "////////////");
                        if (list.getResources().getResourcesname().equals("apis:resources:getList") || list.getResources().getResourcesname().equals("resources")) {
                            logger.info("你刚好有权限标志<------>apis:resources:getList<------>||<------>resources<------>");
                            //这里开始搞事情
                            success.put("content",roleResourcesService.getRoleResourcesList(page, size, sort, operation, key, value));
                            success.put("success",1);
                            return new Payload(success);
//                            return new Payload(roleResourcesService.getRoleResourcesList(page, size, sort, operation, key, value));
                        }
                    }
                } else {
                    error.put("msg","权限为空");
                    error.put("fail","0");
                    return new Payload(error);
//                    return new Payload("权限为空");
                }
            } else {
                error.put("msg","角色为空");
                error.put("fail","0");
                return new Payload(error);
//                return new Payload("角色为空");
            }


        } else {
            error.put("msg","token 过期或者token不存在 请重新登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("token 过期或者token不存在 请重新登陆");
        }
        error.put("msg","不存在/apis/roleResources/getList访问权限");
        error.put("fail","0");
        return new Payload(error);
//        return new Payload("不存在/apis/roleResources/getList访问权限");
    }


    @RequestMapping(value = "/updateByRolename", method = RequestMethod.PUT)//根据id修改
    @ResponseBody
    public Payload updateRoleResourcesByRolename(@QueryParam("sort") @DefaultValue("sort=user_role_id,desc") String rolename,
                                                 @QueryParam("token") @DefaultValue("null") String token,
                                                 @RequestBody Map<String, String[]> jsonObj) {

        List<Token> tokenlist = new ArrayList<Token>();
        if (token == null) {
            error.put("msg","没有登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("没有登陆");
        } else {
            tokenlist = (List<Token>) tokenService.findByTokenname(token);
        }
        if (tokenlist != null && tokenlist.size() > 0) {


            String loginname = tokenlist.get(0).getTokenusername().toString().trim();

            List<UserRole> roleList = (List<UserRole>) userRoleService.getUserRoleListByUsername(loginname);
            if (roleList.size() > 0 && roleList != null) {
                logger.info("你的角色<------>" + roleList.get(0).getRole().getRolename().toString().trim() + "<------>");
                List<RoleResources> resourcesList = (List<RoleResources>) roleResourcesService.getRoleResourcesListByRolename(roleList.get(0).getRole().getRolename().toString().trim());
                if (resourcesList != null && resourcesList.size() > 0) {
                    for (RoleResources list : resourcesList) {
                        logger.info("------目前有的权限" + list.getResources().getResourcesname() + "////////////");
                        if (list.getResources().getResourcesname().equals("apis:resources:updateByRolename") || list.getResources().getResourcesname().equals("resources")) {
                            logger.info("你刚好有权限标志<------>apis:resources:updateByRolename<------>||<------>resources<------>");
                            //这里开始搞事情

                            //这里开始搞事情
                            String[] stringIds = jsonObj.get("resourcesid");
                            if (stringIds != null && stringIds.length > 0) {
                                Integer[] resourcesIds = new Integer[stringIds.length];
                                //转换为int数组
                                for (int i = 0; i < stringIds.length; i++) {
                                    resourcesIds[i] = Integer.parseInt(stringIds[i]);
                                }
                                success.put("content",roleResourcesService.updateRoleResourcesByRolename(rolename, resourcesIds));
                                success.put("success",1);
                                return new Payload(success);
//                                return new Payload(roleResourcesService.updateRoleResourcesByRolename(rolename, resourcesIds));
                            }
                            error.put("msg","false");
                            error.put("fail","0");
                            return new Payload(error);
//                            return new Payload(false);



                        }
                    }
                } else {
                    error.put("msg","权限为空");
                    error.put("fail","0");
                    return new Payload(error);
//                    return new Payload("权限为空");
                }
            } else {
                error.put("msg","角色为空");
                error.put("fail","0");
                return new Payload(error);
//                return new Payload("角色为空");
            }



        } else {
            error.put("msg","token 过期或者token不存在 请重新登陆");
            error.put("fail","0");
            return new Payload(error);
//            return new Payload("token 过期或者token不存在 请重新登陆");
        }
        error.put("msg","不存在/apis/roleResources/updateByRolename访问权限");
        error.put("fail","0");
        return new Payload(error);
//        return new Payload("不存在/apis/roleResources/updateByRolename访问权限");
    }

}
